package com.keti.DaoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.keti.Dao.ApplyPurchaseDao;
import com.keti.pojo.ApplyPurchase;
import com.keti.pojo.stuff;

import util.DBUtil;

public class ApplyPurchaseDaoImpl implements ApplyPurchaseDao{
	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs = null;
	@Override
	public void add(ApplyPurchase ap) {
		conn=DBUtil.getconn();
		String sql="INSERT `apply_purchase`(sname,uid,uname,status,reason) VALUE(?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			String[] params={
					ap.getSname(),
					String.valueOf(ap.getUid()),
					ap.getSname(),
					ap.getStatus(),
					ap.getReason()
				};
				DBUtil.exeUpd(sql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void del() {
		
	}

	@Override
	public List<ApplyPurchase> queryAll() {
		ApplyPurchase ap = null;
		List<ApplyPurchase> list = new ArrayList<ApplyPurchase>();
		conn = DBUtil.getconn();
		try {
			ps = conn.prepareStatement("select *from `apply_purchase`");
			rs = ps.executeQuery();
			stuff s =null;
			while(rs.next()) {
				int id = rs.getInt(1);
				String sname = rs.getString(2);
				int uid = rs.getInt(3);
				String uname = rs.getString(4);
				String status = rs.getString(5);
				String reason = rs.getString(6);
				ap = new ApplyPurchase();
				ap.setId(id);
				ap.setSname(sname);
				ap.setUid(uid);
				ap.setUname(uname);
				ap.setStatus(status);
				ap.setReason(reason);
				list.add(ap);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public ApplyPurchase queryOne(int id) {
		ApplyPurchase ap = new ApplyPurchase();
		List<ApplyPurchase> list = new ArrayList<ApplyPurchase>();
		conn = DBUtil.getconn();
		try {
			ps = conn.prepareStatement("select *from `apply_purchase` where id=?");
			ps.setInt(1, id);
			rs = ps.executeQuery();
			stuff s =null;
			while(rs.next()) {
				id = rs.getInt(1);
				String sname = rs.getString(2);
				int uid = rs.getInt(3);
				String uname = rs.getString(4);
				String status = rs.getString(5);
				String reason = rs.getString(6);
				ap.setId(id);
				ap.setSname(sname);
				ap.setUid(uid);
				ap.setUname(uname);
				ap.setStatus(status);
				ap.setReason(reason);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ap;
	}

	@Override
	public void updata(ApplyPurchase ar) {
		conn = DBUtil.getconn();
		String sql="UPDATE `apply_purchase` set sname=?,uid=?,uname=?,`status`=?,reason=? where id=?";
		try {
			ps = conn.prepareStatement(sql);
			String[] params= {
					ar.getSname(),
					String.valueOf(ar.getUid()),
					ar.getUname(),
					ar.getStatus(),
					ar.getReason(),
					String.valueOf(ar.getId())
			};
			DBUtil.exeUpd(sql, params);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				DBUtil.closeAll(conn, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
